﻿package mortal.game.view.shopMall.shopSecKilling
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;

    public class SeckillingRender extends GCellRenderer
    {
        private var _shopItem:BaseItem;
        private var _propName:GTextFiled;
        private var _oldPrice:BitmapNumberText;
        private var _price:BitmapNumberText;
        private var _deleteLine:GBitmap;
        private var _leftTxt:GTextFiled;
        private var _btn:GLoadingButton;
        private var _buyStateIcon:GImageBitmap;
        private var _info:SPanicBuyItemMsg;
        private var _tshopPanicInfo:TShopPanicBuyItem;

        public function SeckillingRender()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.CENTER;
            _loc_1.size = 14;
            _loc_1.bold = true;
            this._propName = UIFactory.gTextField("", 16, 0, 120, 25, this, _loc_1, true);
            this._shopItem = UICompomentPool.getUICompoment(BaseItem);
            this._shopItem.x = 41;
            this._shopItem.y = 39;
            this._shopItem.setItemStyle(58, ImagesConst.ShopItemBg, 6, 6);
            this.addChild(this._shopItem);
            this._leftTxt = UIFactory.gTextField("", 0, 115, 150, 20, this, GlobalStyle.textFormatLv.center());
            this._oldPrice = UIFactory.bitmapNumberText(75, 144, "BasePassWinNum.png", 18, 21, -8, this, 12, 0);
            this._price = UIFactory.bitmapNumberText(75, 169, "BasePassWinNum.png", 18, 21, -8, this, 12, 0);
            this._deleteLine = UIFactory.gBitmap(ImagesConst.deleteLine, 80, 40 + 112, this);
            this._btn = UIFactory.gLoadingButton(ResFileConst.RechargeShopBigBuy, 19, 240, 108, 44, this);
            this._btn.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._buyStateIcon = UIFactory.gImageBitmap("", 41, 49, this);
            NetDispatcher.addCmdListener(ServerCommand.secKillingItemRecordUpdate, this.updateLeftNum);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.secKillingItemRecordUpdate, this.updateLeftNum);
            this._oldPrice.dispose(param1);
            this._oldPrice = null;
            this._price.dispose(param1);
            this._price = null;
            this._shopItem.dispose(param1);
            this._shopItem = null;
            this._propName.dispose(param1);
            this._propName = null;
            this._deleteLine.dispose(param1);
            this._deleteLine = null;
            this._leftTxt.dispose(param1);
            this._leftTxt = null;
            this._btn.dispose(param1);
            this._btn = null;
            this._buyStateIcon.dispose(param1);
            this._buyStateIcon = null;
            this._info = null;
            this._tshopPanicInfo = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_5:Date = null;
            var _loc_6:Date = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_2:* = this._info.discount;
            var _loc_3:* = Cache.instance.secKilling.sPanicBuyMsg;
            var _loc_4:* = Cache.instance.secKilling.getSelfBuyRecordByItemCode(this._info.itemCode);
            if (_loc_3)
            {
                _loc_5 = _loc_3.panicBuyShop.startDt;
                _loc_6 = _loc_3.panicBuyShop.endDt;
                if (!ClockManager.instance.isWithinTheTime(_loc_5.hours, _loc_5.minutes, _loc_5.seconds, _loc_6.hours, _loc_6.minutes, _loc_6.seconds))
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35552), MsgRollTipsType.msgRollTips3);
                }
                if ((_loc_3.panicBuyItems[this._info.itemCode] as SPanicBuyItemMsg).leftAmount <= 0)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35553), MsgRollTipsType.msgRollTips3);
                }
                if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, _loc_2))
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35108), MsgRollTipsType.msgRollTips3);
                }
                _loc_7 = 1;
                _loc_8 = _loc_4;
                _loc_9 = this._tshopPanicInfo.buyLimit - _loc_8;
                if (_loc_7 > _loc_9)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35554), MsgRollTipsType.msgRollTips3);
                }
                GameProxy.shop.buyPanicItem(_loc_3.panicBuyShop.shopCode, this._info.itemCode, this._shopItem);
            }
            else
            {
                return MsgManager.showRollTipsMsg(Language.getString(35552), MsgRollTipsType.msgRollTips3);
            }
            return;
        }// end function

        private function updateLeftNum(param1:SPanicBuyItemMsg = null) : void
        {
            var _loc_2:int = 0;
            if (param1)
            {
                if (param1.itemCode != this._info.itemCode)
                {
                    return;
                }
                this._info = param1;
            }
            if (this._info)
            {
                _loc_2 = Cache.instance.secKilling.getSelfBuyRecordByItemCode(this._info.itemCode);
                this._leftTxt.htmlText = Language.getStringByParam(35555, _loc_2, this._tshopPanicInfo.buyLimit, this._info.leftAmount);
                this.updateState();
            }
            return;
        }// end function

        private function updateState() : void
        {
            var _loc_4:String = null;
            var _loc_1:* = Cache.instance.secKilling.curShopInfo;
            var _loc_2:* = ClockManager.instance.nowDate;
            var _loc_3:* = _loc_1.endDt;
            if (_loc_2.time >= _loc_3.time)
            {
                this._buyStateIcon.imgUrl = ImagesConst.SecKilling_finish;
                return;
            }
            var _loc_5:* = Cache.instance.secKilling.getSelfBuyRecordByItemCode(this._info.itemCode);
            var _loc_6:* = this._tshopPanicInfo.buyLimit;
            if (_loc_5 >= _loc_6)
            {
                this._buyStateIcon.imgUrl = ImagesConst.SecKilling_hasBuy;
                return;
            }
            if (this._info.leftAmount <= 0)
            {
                this._buyStateIcon.imgUrl = ImagesConst.SecKilling_SellOut;
                return;
            }
            this._buyStateIcon.imgUrl = "";
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._info = param1 as SPanicBuyItemMsg;
            var _loc_2:* = Cache.instance.secKilling.shopCode;
            this._tshopPanicInfo = ShopConfig.instance.getRebatShopInfoByKey(_loc_2 + "_" + this._info.itemCode);
            var _loc_3:* = new ItemData(this._info.itemCode);
            this._shopItem.itemData = _loc_3;
            this._propName.htmlText = _loc_3.itemInfo.htmlName;
            this._price.text = this._info.discount.toString();
            this._oldPrice.text = this._tshopPanicInfo.price.toString();
            this.updateLeftNum();
            return;
        }// end function

    }
}
